package com.ruoyi.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.SysTableColumn;
import com.ruoyi.system.domain.TFormConfig;
import com.ruoyi.system.service.ISysTableColumnService;
import com.ruoyi.system.service.ITFormConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysTable;
import com.ruoyi.system.service.ISysTableService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 表扩展Controller
 * 
 * @author ruoyi
 * @date 2024-03-25
 */
@Api("扩展表管理")
@RestController
@RequestMapping("/system/table")
public class SysTableController extends BaseController
{
    @Autowired
    private ISysTableService sysTableService;
    @Autowired
    private ISysTableColumnService sysTableColumnService;
    @Autowired
    private ITFormConfigService tFormConfigService;

    /**
     * 查询表扩展列表
     */
    @ApiOperation("获取列表")
    @PreAuthorize("@ss.hasPermi('system:table:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTable sysTable)
    {
        startPage();
        List<SysTable> list = sysTableService.selectSysTableList(sysTable);
        return getDataTable(list);
    }

    @ApiOperation("获取数据库表")
    @GetMapping("/db/list")
    public TableDataInfo dbList(SysTable sysTable) {
        startPage();
        List<SysTable> list = sysTableService.selectDbList(sysTable);
        return getDataTable(list);
    }
    @ApiOperation("导入数据库表")
    @PostMapping("/importTable")
    public AjaxResult importTable(String tables) {
        String[] tableNames = Convert.toStrArray(tables);
        // 查询表信息
        List<SysTable> tableList = sysTableService.selectDbTableListByNames(tableNames);
        sysTableService.importTable(tableList, super.getUsername());
        return success();
    }

    /**
     * 导出表扩展列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:export')")
    @Log(title = "表扩展", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTable sysTable)
    {
        List<SysTable> list = sysTableService.selectSysTableList(sysTable);
        ExcelUtil<SysTable> util = new ExcelUtil<SysTable>(SysTable.class);
        util.exportExcel(response, list, "表扩展数据");
    }

    /**
     * 获取表扩展详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SysTable table = sysTableService.selectSysTableById(id);
        SysTableColumn queryColumn = new SysTableColumn();
        queryColumn.setTableId(id);
        List<SysTableColumn> columns = sysTableColumnService.selectSysTableColumnList(queryColumn);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("table", table);
        map.put("columns", columns);
        return success(map);
    }

    @GetMapping("/infoByName")
    public AjaxResult infoByName(String tableName) {
        SysTable table = sysTableService.selectOne("table_name", tableName);

        if(table == null) {
            return AjaxResult.error(tableName + "不存在！");
        }

        SysTableColumn queryColumn = new SysTableColumn();
        queryColumn.setTableId(table.getId());
        List<SysTableColumn> columns = sysTableColumnService.selectSysTableColumnList(queryColumn);


        TFormConfig tFormConfig = tFormConfigService.selectOne("table_name", tableName);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("table", table);
        map.put("columns", columns);
        map.put("form", tFormConfig);
        return success(map);
    }
    /**
     * 新增表扩展
     */
    @PreAuthorize("@ss.hasPermi('system:table:add')")
    @Log(title = "表扩展", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTable sysTable)
    {
        return toAjax(sysTableService.insertSysTable(sysTable));
    }

    /**
     * 修改表扩展
     */
    @PreAuthorize("@ss.hasPermi('system:table:edit')")
    @Log(title = "表扩展", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTable sysTable)
    {
        return toAjax(sysTableService.updateSysTable(sysTable));
    }

    /**
     * 删除表扩展
     */
    @PreAuthorize("@ss.hasPermi('system:table:remove')")
    @Log(title = "表扩展", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysTableService.deleteSysTableByIds(ids));
    }



}
